/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.real_name;


import com.yuecai.modules.yc_real_name.domain.YcRealName;
import com.yuecai.modules.yc_real_name.service.YcRealNameService;
import com.yuecai.modules.yc_real_name.service.dto.YcRealNameDto;
import com.yuecai.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
* @website https://el-admin.vip
* @author dm
* @date 2020-06-23
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "yc_real_name管理")
@RequestMapping("/api/yc-real-name")
public class YcRealNameController {

    private final YcRealNameService ycRealNameService;


    @GetMapping
    @ApiOperation("查询yc_real_name")
    public ResponseEntity<Object> query(){
        Long currentUserId = SecurityUtils.getCurrentUserId();
        YcRealNameDto byUserId = ycRealNameService.findByUserId(currentUserId);
        if (byUserId==null){
            return new ResponseEntity<>("您未认证！",HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity<>(byUserId,HttpStatus.OK);
    }

    @PostMapping
    @ApiOperation("新增yc_real_name")
    public ResponseEntity<Object> create(@Validated @RequestBody YcRealName resources){
        return new ResponseEntity<>(ycRealNameService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @ApiOperation("修改yc_real_name")
    public ResponseEntity<Object> update(@Validated @RequestBody YcRealName resources){
        Long currentUserId = SecurityUtils.getCurrentUserId();
        resources.setUserId(currentUserId);
        ycRealNameService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    @ApiOperation("删除yc_real_name")
    @DeleteMapping
    public ResponseEntity<Object> delete() {
        ycRealNameService.delete();
        return new ResponseEntity<>(HttpStatus.OK);
    }
}